package com.quanyan.club.task.tracker;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.activity.entity.ActivityInfo;
import com.quanyan.activity.service.ActivityOpenService;
import com.quanyan.club.entity.db.TbSignUser;
import com.quanyan.club.entity.db.TbSignUserExample;
import com.quanyan.club.mapper.TbSignUserMapper;
import com.quanyan.club.service.CppService;
import com.quanyan.common.constants.Constants;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2016/7/8.
 *
 * 定时执行任务，活动创建时，提交此任务 此定时任务为活动开始前半小时执行，如果此活动没有被取消或暂停，发送通知给已报名的用户
 */
@Component
public class ActivityStartNotifyJob implements JobRunner {

    private static Logger logger = Logger.getLogger(ActivityStartNotifyJob.class);

    @Autowired
    private ActivityOpenService activityOpenService;
    @Autowired
    private TbSignUserMapper signUserMapper;
    @Autowired
    private CppService cppService;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        Job job = jobContext.getJob();
        if (job == null) {
            return new Result(Action.EXECUTE_EXCEPTION, "任务不存在");
        }
        Map<String, String> params = job.getExtParams();
        logger.warn("俱乐部活动开始前通知任务执行，参数：" + params);

        //1. 获取活动信息，俱乐部信息
        int activityId = Integer.valueOf(params.get("activityId"));
        int clubId = Integer.valueOf(params.get("clubId"));

        //2. 判断此俱乐部是否已经被取消，暂停
        ActivityInfo activityInfo = activityOpenService.selectOneActivityById(activityId);
        logger.warn("activityInfo=" + JSON.toJSONString(activityInfo));

        if (activityInfo != null &&
                (activityInfo.getStatus() != 0 || activityInfo.getIsDelete() == 1)) {
            //活动被取消，或者暂停
            logger.warn("活动被取消或暂停！");
            return new Result(Action.EXECUTE_SUCCESS);
        }

        //3. 调用消息推送系统，推送活动即将开始的消息给已报名的人
        TbSignUserExample signUserExample = new TbSignUserExample();
        signUserExample.createCriteria()
                .andActivityIdEqualTo(activityId)
                .andIsCancelEqualTo(Constants.ZERO)
                .andStatusEqualTo(1);//有效
        List<TbSignUser> signUsers = signUserMapper.selectByExample(signUserExample);
        if(null == signUsers || signUsers.size() == 0)
        {
            logger.warn("该活动没有已报名的人");
            return new Result(Action.EXECUTE_SUCCESS);
        }
        for (TbSignUser signUser : signUsers) {
            int orderUid = signUser.getOrderUserId();//只发送给活动报名主要信息人
            logger.warn("开始推送活动开始消息给报名人：" + signUser.getName());
            cppService.pushActivityStartingMsg(activityInfo, Arrays.asList(orderUid));
            logger.warn("已经推送活动开始消息给报名人：" + signUser.getName());
        }

        return new Result(Action.EXECUTE_SUCCESS);
    }
}
